package cn.shop.cloud.web.advice;

import cn.shop.cloud.common.BusinessException;
import cn.shop.cloud.common.CommonControllerAdvice;
import cn.shop.cloud.common.Result;
import cn.shop.cloud.web.msg.MemberCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @program: work
 * @ClassName MemberServerCintrollerAdvice
 * @description:
 * @author: 李尧坤
 * @create: 2024−12-26 11:31
 * @Version 1.0
 **/
@ControllerAdvice
public class MemberServerControllerAdvice extends CommonControllerAdvice {
  /*  @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerBusinessException(BindException ex){
           //拿到默认的信息
        String msg = ex.getAllErrors().get(0).getDefaultMessage();
          //把信息进行填充
        return Result.success(MemberCodeMsg.PARAM_ERROR.fillArgs(msg));
    }*/

    //定义当前项目自定义异常
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handleBusinessException(BindException ex){
        //如何获取到异常信息.？
        //BindException会把所有的错误消息都存储在这个BindException中
        // ex.getAllErrors()获取到所有的异常信息,只需要告诉前台第一个错误信息
        String msg = ex.getAllErrors().get(0).getDefaultMessage();
        //错误状态码和信息应该绑定的关系.
        //500103,参数校验: 手机号码格式不正确
        //500103,参数校验: 密码不能为空
        // codeMsg.getMsg()====>参数校验:{0}
        return Result.error(MemberCodeMsg.PARAM_ERROR.fillArgs(msg));
    }

}
